package com.cnebula.uas.authtoken.service.impl;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.cnebula.uas.authtoken.service.IdpService;

public class IdpServiceImpl implements IdpService {

	@Override
	public void validLogin(String idpCode, String artifact) throws Exception{
		
		//TODO
		//1. 请求idp，获取用户信息
		String loginValidURL = "http://210.47.255.5:8080/calis/GetReaderInfo.php";
		
		URIBuilder builder = new URIBuilder(loginValidURL);
		builder.setParameter("artifact", artifact);
		
		URI url = builder.build();
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		httpGet.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
		CloseableHttpResponse res = client.execute(httpGet);
		HttpEntity entity = res.getEntity();

		String content = EntityUtils.toString(entity, "utf-8");
		res.close();
		
		
		//2. 转换用户信息到okapi
		//3. 创建或更新用户信息到mod-user
		//4. 创建或更新用户权限到mod-permission
		
		

	}

}
